<template>
  <div class="main-wrapper" ref="mainWrapper">

      <router-view></router-view>

      <div v-transfer-dom>
        <popup v-model="sidebarPopup"
        position="left"
        @on-hide="sidebarHide"
        @on-show="sidebarShow"
        width="70%">
            <div class="sidebar-content">
              <div class="user-info">
                <img src="../../assets/avatar.jpg" class="avatar" alt="">
                <span class="name">Dave_Xue</span>
              </div>
              <div class="menubar">
                <div class="left" @click="goToCollect">
                  <i class="icon iconfont icon-shoucang"></i><br>
                  <span>收藏</span>
                </div>
                <div>
                  <i class="icon iconfont icon-xiaoxi"></i><br>
                  <span>消息</span>
                </div>
                <div class="right">
                  <i class="icon iconfont icon-shezhi"></i><br>
                  <span>设置</span>
                </div>
              </div>
              <div class="menu-list-wrapper">
                <scroller ref="navScroll"
                class="nav-menu"
                :probe-type="2"
                :listen-scroll="listenScroll"
                :data="sidebarData">
                  <ul>
                    <li class="nav-item" :class="{'active': item.id===currentThemeId}" v-for="item in sidebarData" :key="item.id" @click="changeTheme(item.id)">
                      <i class="icon iconfont icon-shouyeshouye" v-if="item.id===-1"></i>
                      {{item.name}}
                      <i class="icon iconfont icon-more"></i>
                    </li>
                  </ul>
                </scroller>
              </div>
              <div class="foot-menu">
                <div>
                  <i class="icon iconfont icon-lixianwenjian"></i><span>离线</span>
                </div>
                <div class="right">
                  <i class="icon iconfont icon-yejianmoshi"></i><span>夜间</span>
                </div>
              </div>
            </div>
        </popup>
      </div>

  </div>
</template>
<script>
import { TransferDom, Popup } from "vux";
import axios from "axios";
import Swiper from "components/swiper/swiper";
import NewsList from "components/newsList/newsList";
import Scroller from "components/scroller/scroller";
import { log } from "util";
import { mapGetters, mapMutations } from "vuex";
export default {
  directives: {
    TransferDom
  },
  data() {
    return {
      sidebarData: []
    };
  },
  computed: {
    sidebarPopup: {
      get() {
        return this.$store.state.sidebarStatu;
      },
      set() {}
    },
    ...mapGetters(["currentThemeId"])
  },
  created() {
    this.listenScroll = true;
  },
  mounted() {
    this.getSidebarNavData();
    setTimeout(() => {
      this.$refs.navScroll.refresh();
    }, 20);
  },
  methods: {
    scroll(pos) {
      this.scrollY = pos.y;
    },
    changeTheme(id) {
      // 点击修改主题
      // 修改当前选择主题id
      this.changeCurrentId(id);
      if (id === -1) {
        // 切换到首页
        this.$router.push("/");
      } else {
        this.$router.push({ name: "theme", params: { id: id } });
      }
      // 切换主题并关闭侧边栏
      this.changeSidebarStatu(false);
    },
    getSidebarNavData() {
      // 获取侧边栏导航数据
      axios
        .get("api/themes")
        .then(res => {
          let localOthers = [
            {
              "color": 8307764,
              "thumbnail": "http://pic4.zhimg.com/2c38a96e84b5cc8331a901920a87ea71.jpg",
              "description": "内容由知乎用户推荐，海纳主题百万，趣味上天入地",
              "id": 12,
              "name": "用户推荐日报"
            },
          ]
          this.sidebarData = localOthers;// res.data.others;
          this.sidebarData.unshift({
            color: 0,
            thumbnail: "",
            description: "首页",
            id: -1,
            name: "首页"
          });
        })
        .catch(err => {
          console.log(err);
        });
    },
    sidebarHide() {
      // 获取侧边栏关闭事件
      this.$refs.mainWrapper.style["transform"] = `translate3d(0px, 0, 0)`;
      this.changeSidebarStatu(false);
    },
    sidebarShow() {
      // 获取侧边栏打开事件
      const offsetWidth = document.body.clientWidth * 0.7;
      this.$refs.mainWrapper.style[
        "transform"
      ] = `translate3d(${offsetWidth}px, 0, 0)`;
    },
    goToCollect() {
      this.$router.push('/collect');
    },
    ...mapMutations({
      changeSidebarStatu: "CHANGE_SIDEBAR_STATU",
      changeCurrentId: "CHANGE_CURRENT_THEME_ID"
    })
  },
  components: {
    Scroller,
    Swiper,
    NewsList,
    Popup
  }
};
</script>
<style lang="less">
.weui-cells {
  margin-top: 0 !important;
}
.weui-cells:before {
  border-top: 0 !important;
}
.vux-popup-dialog.vux-popup-left {
  color: #94999d;
  background-color: #232a30;
  overflow-y: hidden;
}
.main-wrapper {
  width: 100%;
  height: 100%;
  transition: all 300ms;
  .header {
    position: fixed;
    width: 100%;
    height: 32px;
    line-height: 32px;
    z-index: 5;
    font-size: 20px;
    color: #fff;
    // background-color: #028fd6;
    background-color: transparent;
    .icon-fenlei {
      height: 100%;
      font-size: 26px;
      padding: 0 15px;
    }
    .hot-news {
      position: absolute;
      width: 90px;
      height: 100%;
      top: 0;
      left: 50%;
      margin-left: -45px;
      text-align: center;
    }
  }
  .scroll-wrap {
    position: absolute;
    width: 100%;
    top: 0px;
    bottom: 0;
    .news {
      height: 100vh;
    }
  }
}

.sidebar-content {
  padding-left: 20px;
  padding-right: 40px;
  box-sizing: border-box;
  .user-info {
    height: 60px;
    line-height: 60px;
    font-size: 0;
    .avatar {
      width: 36px;
      height: 36px;
      border-radius: 50%;
      margin-right: 12px;
      vertical-align: -14px;
    }
    .name {
      font-size: 15px;
    }
  }
  .menubar {
    height: 60px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    div {
      width: 33.3%;
      text-align: center;
      font-size: 0;
      &.left {
        text-align: left;
      }
      &.right {
        text-align: right;
      }
      .icon {
        font-size: 24px;
      }
      span {
        font-size: 12px;
        line-height: 9px;
      }
    }
  }
  .menu-list-wrapper {
    position: absolute;
    width: 100%;
    left: 0;
    right: 0;
    top: 120px;
    bottom: 40px;
    box-sizing: border-box;
    &::-webkit-scrollbar {
      display: none;
    }
    .nav-menu {
      height: 100%;
      overflow: hidden;
      ul {
        margin: 0;
        padding: 0;
        list-style: none;
        .nav-item {
          height: 50px;
          line-height: 50px;
          font-size: 14px;
          padding-left: 20px;
          padding-right: 33px;
          &.active {
            background-color: #1b222a;
            color: #fff;
          }
          .icon-shouyeshouye {
            font-size: 20px;
            margin-right: 8px;
          }
          .icon-more {
            float: right;
          }
        }
      }
    }
  }
  .foot-menu {
    width: 100%;
    box-sizing: border-box;
    padding: 0 40px 0 20px;
    position: absolute;
    height: 40px;
    line-height: 40px;
    left: 0;
    bottom: 0;
    div {
      width: 50%;
      float: left;
      &.right {
        text-align: right;
      }
      span {
        font-size: 13px;
        margin-left: 6px;
      }
    }
  }
}
</style>
